package com.heima.controller;

import com.heima.pojo.Result;
import com.heima.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        //把文件的内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，从而防止文件覆盖
        String filename = UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));

        //存储到本地的磁盘
        file.transferTo(new File("D:\\aaa_study_tool\\files\\"+filename));
//        存储到阿里云
//        String url = AliOssUtil.uploadFile(filename,file.getInputStream());
//        return Result.success(url);
        return Result.success("https://img.wxcha.com/m00/83/0a/f735e43e6faa531f92314ee8df068543.jpg?down");
    }
}
